package de.bib.blue.manage.finance.dialog;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import de.bib.blue.manage.finance.util.UIHelper;

public class AbstractUserDialog extends TitleAreaDialog {

	/**
	 * Button untuk OK.
	 */
	protected Button ok = null;

	protected Text usernameText = null;

	protected Text passwordText = null;

	private String username;

	private String password;

	private final String title;

	public AbstractUserDialog(Shell parentShell, String title) {
		super(parentShell);
		this.title = title;
	}

	protected Control createNewContents(Composite parent, String message) {
		Control contents = super.createContents(parent);
		setTitle(title);
		setMessage(message);
		

		/*
		 * Button ok di-hidden agar user tidak salah login.
		 */
		ok = getButton(IDialogConstants.OK_ID);
		ok.setEnabled(false);

		return contents;
	}

	protected void createPassword(Composite container) {
		passwordText = createPassword(container, "Password:");
	}

	protected Text createPassword(Composite container, String label) {
		createLabel(container, label);
		Text passwordText = new Text(container, SWT.BORDER | SWT.PASSWORD);
		passwordText.setLayoutData(UIHelper.fillHorizontal());
		passwordText.addModifyListener(getTextModifyListener());
		return passwordText;
	}

	protected void createUsername(Composite container) {
		createLabel(container, "Username:");
		usernameText = createText(container);
		usernameText.addModifyListener(getTextModifyListener());
	}

	protected Label createLabel(Composite parent, String text) {
		return UIHelper.createLabel(parent, text);
	}

	protected Text createText(Composite container) {
		return UIHelper.createText(container);
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("Finance Monitoring");
	}

	protected void error(String message) {
		ok.setEnabled(false);
		setErrorMessage(message);
	}

	/**
	 * Method untuk mengecek apakah teks username dan password sudah lengkap.
	 * Bila sudah button OK akan ditampilkan.
	 */
	protected void checkCompleted() {
		if (usernameText != null) {
			if (usernameText.getText().length() > 0 && passwordText != null) {
				if (passwordText.getText().length() > 0) {
					ok.setEnabled(true);
					username = usernameText.getText();
					password = passwordText.getText();
					return;
				}
			}
		}

		if (ok != null) {
			ok.setEnabled(false);
		}
	}

	/**
	 * Ambil username yang di ketik oleh user.
	 * 
	 * @return username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * Ambil password yang di ketik user.
	 * 
	 * @return password
	 */
	public String getPassword() {
		return password;
	}

	protected ModifyListener getTextModifyListener() {
		return new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e) {
				checkCompleted();
			}
		};
	}
}
